<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class System extends Admin_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('model_setting', 'setting' ,TRUE); //载入配置模型
		$this->load->model('model_user', 'user' ,TRUE); //载入会员模型
	}
	
	public function index()
	{

		$this->setting();
			 
	}
 
	 
	/**
	 * 系统设置页面
	 */
	public function setting(){
		
		if(isset($_POST['submit'])){
			
			//更新数据
			$post = $this->input->post();
			
			foreach ($post as $key =>$value) {
				$this->setting->primaryKey = 'skey';
				$this->setting->save(array('svalue' => $value),$key);
			}
			
			//重写缓存
			write_cache(TRUE);
			$this->_message("修改成功",'system/setting', TRUE );
		 
		}
		else 
		{
			$settings = $this->setting->get_settings();
			$this->_template('setting',$settings);
		}
		
	}
	
	
	
	/**
	 * 修改密码
	 */
	public function password(){
		
		if(isset($_POST['submit'])){
			$this->load->helper('security');
			//更新数据
			$oldpassword = $this->input->post('oldpassword');
			$password = $this->input->post('password');
			$id = $this->input->post('id');
			
			$row  = $this->user->find(" password =  '".do_hash($oldpassword)."' " );
			
			if(!$password){$this->session->set_flashdata('error', "密码不能为空");redirect(made_admin_url('system/password/'.$id));}
			
			
			if($row)
			{
				if($this->user->save(array('password'=>do_hash($password)),$id))
				{
					$this->_message("密码修改成功!",'system/password/'.$id, TRUE );
				}
				else 
				{
					$this->_message("修改失败，请检查填写是否错误!",'system/password/'.$id, TRUE );
				}
			}
			else
			{
				$this->_message("原密码错误，请返回重新修改!",'system/password/'.$id, TRUE );
			}
			
			
		 
		}
		else 
		{
			
			$id = $this->uri->segment(4);
			//取得单条信息项目
			$data['items'] = $this->user->read($id);
			
			
			/*简单权限判断  以后完善
			if($data['items']['flag'] != 999)
			{
				$this->session->set_flashdata('error', '您不是管理员,不能修改!');
				redirect(made_admin_url('login'));	
			}*/
			 
			
			$this->_template('user_edit_password.php',$data);
		}
		
		
		
	} 
	
	
	public function update_cache(){
		
		write_cache(TRUE);
		$this->_message("更新缓存成功",'',FALSE);
		
	}
	
	
	
	public function dataconfig(){
		
		$this->_message("数据库配置请修改 /config/config.php",'',FALSE);
		
	}

    public function seo(){

        $this->_message("此功能为商业定制功能",'',FALSE);

    }

    public function kefu(){

        $this->_message("此功能为商业定制功能",'',FALSE);

    }
	
	 
	
	
	 
}
